import Vue from "vue";
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

const state = {
  count:0,
  price:3599,
  movieList:[]
}
const mutations = {
  increment(state){
    state.count++
  },
  incrementN(state,{n}){
    state.count+=n
  },
  setMovieList(state,value){
    state.movieList = value
  }
}
const actions = {
  incrementWait({commit}){
    setTimeout(() => {
      commit('increment')
    },2000)
  },
  incrementNWait({commit},payload){
    setTimeout(() => {
      commit('incrementN',payload)
    },2000)
  },
  async getMovieList({commit}){
    const result = await axios.get("https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=2&ret_num=48&session=b9fd987164f6aa47fad266f57dffaa6a")
    commit('setMovieList',result.data.data.list)
  }
}
const getters = {
  myMovieList(state){
    return state.movieList.filter(item => {
      return item.albumName.length > 3 && item.albumName.length < 7
    })
  },
  yourMovieList(state){
    return ({start,end}) => {
      return state.movieList.filter(item => {
        return item.albumName.length >= start && item.albumName.length <= end
      })
    }
  }
}
export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters
})